{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 测试ollama是否调通"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "ename": "JSONDecodeError",
     "evalue": "Extra data: line 1 column 5 (char 4)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mJSONDecodeError\u001b[0m                           Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[7], line 21\u001b[0m\n\u001b[0;32m      8\u001b[0m \u001b[38;5;66;03m#     response_content = response_dict[\"response\"]\u001b[39;00m\n\u001b[0;32m      9\u001b[0m \u001b[38;5;66;03m#     return response_content\u001b[39;00m\n\u001b[0;32m     11\u001b[0m data \u001b[38;5;241m=\u001b[39m {\n\u001b[0;32m     12\u001b[0m     \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmodel\u001b[39m\u001b[38;5;124m\"\u001b[39m: \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mqwen2.5:14b\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[0;32m     13\u001b[0m     \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmessages\u001b[39m\u001b[38;5;124m\"\u001b[39m: [\n\u001b[1;32m   (...)\u001b[0m\n\u001b[0;32m     17\u001b[0m     \n\u001b[0;32m     18\u001b[0m }\n\u001b[1;32m---> 21\u001b[0m res \u001b[38;5;241m=\u001b[39m \u001b[43mget_response\u001b[49m\u001b[43m(\u001b[49m\u001b[43murl_generate\u001b[49m\u001b[43m,\u001b[49m\u001b[43mdata\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m     22\u001b[0m \u001b[38;5;28mprint\u001b[39m(res)\n",
      "Cell \u001b[1;32mIn[7], line 6\u001b[0m, in \u001b[0;36mget_response\u001b[1;34m(url, data)\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mget_response\u001b[39m(url, data):\n\u001b[0;32m      5\u001b[0m     response \u001b[38;5;241m=\u001b[39m requests\u001b[38;5;241m.\u001b[39mpost(url, json\u001b[38;5;241m=\u001b[39mdata)\n\u001b[1;32m----> 6\u001b[0m     response_dict \u001b[38;5;241m=\u001b[39m \u001b[43mjson\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mloads\u001b[49m\u001b[43m(\u001b[49m\u001b[43mresponse\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtext\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m      7\u001b[0m     \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m模型回复:\u001b[39m\u001b[38;5;124m\"\u001b[39m,response_dict)\n",
      "File \u001b[1;32md:\\Python312\\Lib\\json\\__init__.py:346\u001b[0m, in \u001b[0;36mloads\u001b[1;34m(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)\u001b[0m\n\u001b[0;32m    341\u001b[0m     s \u001b[38;5;241m=\u001b[39m s\u001b[38;5;241m.\u001b[39mdecode(detect_encoding(s), \u001b[38;5;124m'\u001b[39m\u001b[38;5;124msurrogatepass\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m    343\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m (\u001b[38;5;28mcls\u001b[39m \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mand\u001b[39;00m object_hook \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mand\u001b[39;00m\n\u001b[0;32m    344\u001b[0m         parse_int \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mand\u001b[39;00m parse_float \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mand\u001b[39;00m\n\u001b[0;32m    345\u001b[0m         parse_constant \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mand\u001b[39;00m object_pairs_hook \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m kw):\n\u001b[1;32m--> 346\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43m_default_decoder\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdecode\u001b[49m\u001b[43m(\u001b[49m\u001b[43ms\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m    347\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mcls\u001b[39m \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m    348\u001b[0m     \u001b[38;5;28mcls\u001b[39m \u001b[38;5;241m=\u001b[39m JSONDecoder\n",
      "File \u001b[1;32md:\\Python312\\Lib\\json\\decoder.py:340\u001b[0m, in \u001b[0;36mJSONDecoder.decode\u001b[1;34m(self, s, _w)\u001b[0m\n\u001b[0;32m    338\u001b[0m end \u001b[38;5;241m=\u001b[39m _w(s, end)\u001b[38;5;241m.\u001b[39mend()\n\u001b[0;32m    339\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m end \u001b[38;5;241m!=\u001b[39m \u001b[38;5;28mlen\u001b[39m(s):\n\u001b[1;32m--> 340\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m JSONDecodeError(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mExtra data\u001b[39m\u001b[38;5;124m\"\u001b[39m, s, end)\n\u001b[0;32m    341\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m obj\n",
      "\u001b[1;31mJSONDecodeError\u001b[0m: Extra data: line 1 column 5 (char 4)"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import json\n",
    "url_generate = \"http://192.168.20.43:11434/api/chat\"\n",
    "def get_response(url, data):\n",
    "    response = requests.post(url, json=data)\n",
    "    response_dict = json.loads(response.text)\n",
    "    print(\"模型回复:\",response_dict)\n",
    "#     response_content = response_dict[\"response\"]\n",
    "#     return response_content\n",
    "\n",
    "data = {\n",
    "    \"model\": \"qwen2.5:14b\",\n",
    "    \"messages\": [\n",
    "    { \"role\": \"user\", \"content\": \"你好\" }\n",
    "  ],\n",
    "    \"stream\": False\n",
    "    \n",
    "}\n",
    "\n",
    "\n",
    "res = get_response(url_generate,data)\n",
    "print(res)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 使用ollama调用crewai\n",
    "\n",
    "参见：https://crewai.theforage.cn/how-to/LLM-Connections/?h=ollama#ollama\n",
    "更新langchian_openai\n",
    "pip install --upgrade langchain_openai\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING:opentelemetry.trace:Overriding of current TracerProvider is not allowed\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "今日，天穹无垠而澄澈，金色的日光如同倾泻的琼浆玉液，洒落在万物之上。空气中弥漫着温暖而又轻盈的气息，令人心旷神怡。阳光透过稀疏的云层，映照出一片辉煌灿烂的世界，仿佛是大自然最精致细腻的一幅画卷。\n"
     ]
    }
   ],
   "source": [
    "from crewai import Agent, Task, Crew, LLM, Process\n",
    "\n",
    "my_llm = LLM(\n",
    "    api_key = \"NA\",\n",
    "    model = \"ollama/qwen2.5:14b\",\n",
    "    base_url = \"http://192.168.20.43:11434\"\n",
    ")\n",
    "\n",
    "# 定义 Agent\n",
    "agent = Agent(\n",
    "    name=\"MyAgent\",\n",
    "    role=\"文本描述专家\",  # 角色\n",
    "    goal=\"根据给定的文本生成详细的描述\",  # 目标\n",
    "    backstory=\"\"\"我是一个专业的文本描述专家，擅长根据提供的文本内容生成详细、准确的描述，引人入圣。\"\"\",\n",
    "    llm=my_llm\n",
    ")\n",
    "\n",
    "# 定义任务\n",
    "task = Task(\n",
    "    description=\"为以下文本提供全新的描写角度和细节：'今天天气晴朗，阳光明媚。'\",\n",
    "    agent=agent,\n",
    "    expected_output=\"一段更有文采的文本描述\"\n",
    ")\n",
    "\n",
    "# 创建 Crew 并运行\n",
    "crew = Crew(\n",
    "    agents=[agent], \n",
    "    tasks=[task], \n",
    "    full_output=True    )\n",
    "\n",
    "result = crew.kickoff()\n",
    "\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 使用litellm调用ollama模型\n",
    "测试用例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ModelResponse(id='chatcmpl-286df138-9f83-42d2-9efb-263829ea5e75', created=1738915833, model='ollama/qwen2.5:14b', object='chat.completion', system_fingerprint=None, choices=[Choices(finish_reason='stop', index=0, message=Message(content='我是阿里云开发的语言模型Qwen，旨在提供帮助和解答问题。', role='assistant', tool_calls=None, function_call=None, provider_specific_fields=None))], usage=Usage(completion_tokens=17, prompt_tokens=41, total_tokens=58, completion_tokens_details=None, prompt_tokens_details=None))\n"
     ]
    }
   ],
   "source": [
    "from litellm import completion\n",
    "    \n",
    "response = completion(\n",
    "    model = 'ollama/qwen2.5:14b',\n",
    "    messages=[{ \"content\": \"用20个字介绍你是谁?\",\"role\": \"user\"}], \n",
    "    api_base=\"http://192.168.20.43:11434\"\n",
    ")\n",
    "print(response)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
